package com.cwj.text;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Array;
import java.util.ArrayList;

public class splitData {
    public static void main(String[] args) throws IOException {
        String path = "D:\\学习资料\\计组\\计算机硬件设计实验\\hustzc-master\\" +
                "hustzc-master\\5.存储系统实验\\16点阵字库文件分拆\\HZK16_1.txt";
        int NUM = 4;
        split_plus(path, NUM);
    }

    static void split(String path, int NUM) throws IOException {
        File file = new File(path);
        ArrayList<String> list = new ArrayList<>();
        String head = null;
        for (int i = 0; i < NUM; i++) {
            list.add(path + "_" + i);
        }
        try (FileReader in = new FileReader(file, StandardCharsets.UTF_8);
             LineNumberReader lineNumberReader = new LineNumberReader(in);
             BufferedReader inLine = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
        ) {
            long len = file.length();
            lineNumberReader.skip(len);
            long Line_size = lineNumberReader.getLineNumber() - 1;
            head = inLine.readLine();
            long sizePre = Line_size / 4;
            if (Line_size % 4 == 1) {
                sizePre = Line_size / 4 + 1;
            }
            for (int i = 0; i < NUM; i += 1) {
                String str = null;
                try (FileWriter out = new FileWriter(list.get(i), StandardCharsets.UTF_8);
                     BufferedWriter outLine = new BufferedWriter(out);) {
                    outLine.write(head);
                    outLine.newLine();
                    for (int j = 0; j < sizePre; j += 1) {
                        if ((str = inLine.readLine()) != null) {
                            outLine.write(str);
                            outLine.newLine();
                        }
                    }
                }

            }
        }
    }

    static void split_plus(String path, int NUM) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < NUM; i++) {
            list.add(path + "_" + i);
        }
        File file = new File(path);
        BufferedReader in = null;
        ArrayList<BufferedWriter> outList = new ArrayList<>();
        try {
            in = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));

            for (int i = 0; i < NUM; i++) {
                outList.add(new BufferedWriter(new FileWriter(list.get(i))));
            }
            String head = in.readLine();
            for (int i = 0; i < outList.size(); i++) {
                outList.get(i).write(head);
                outList.get(i).newLine();
            }
            String str=null;
            int top=0;
            int size=outList.size();
            while((str=in.readLine())!=null){
                outList.get(top%size).write(str);
                outList.get(top%size).newLine();
                top+=1;
            }
            for (int i = 0; i < outList.size(); i++) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                for (int i = 0; i < outList.size(); i += 1) {
                    outList.get(i).close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}